package com.evl.task;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.evl.common.persistence.dao.TrainOrderMapper;
import com.evl.common.persistence.model.TrainOrder;
import com.evl.trainOrder.service.ITrainOrderService;

/**
 * 定时刷新超时未支付的订单
 * @author dangsheng
 *
 */
@Component
public class OrderRefreshTask {
	
	@Resource
	private TrainOrderMapper trainOrderMapper;
	
	@Resource
	ITrainOrderService iTrainOrderService;
	
	private Logger log = Logger.getLogger(this.getClass());
	
	@Scheduled(cron="0 0/2 * * * ?") //间隔2分钟执行
	public void refreshOrder(){
		log.info("定时任务:撤销30分钟未支付订单开始");
		try {
			refreshOrderTransactional();
		} catch (Exception e) {
			log.error("定时失效未支付订单失败:"+e.getMessage());
			e.printStackTrace();
		}
		log.info("定时任务:撤销30分钟未支付订单完成");
	}
	
	private void refreshOrderTransactional() throws Exception{
		try{
			//查询超过30分钟未支付的订单
			log.debug("查询超时订单");
			List<TrainOrder> unPayOrderList = trainOrderMapper.findUnPayOrders();
			//失效订单
			iTrainOrderService.invaliOrderList(unPayOrderList);
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
	}
}
